package _18_剑指OfferII;

import java.util.*;

public class _033_剑指OfferII变位词组 {

    // 只要能保证异位字母的唯一性就可以确定数据
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<Integer, List<String>> map = new HashMap<>();

        for (String str : strs) {
            int[] ins = new int[26];
            for (int i = 0; i < str.length(); i++) {
                ins[str.charAt(i) - 'a']++;
            }
            int key = Arrays.hashCode(ins);
            List<String> list = map.getOrDefault(key, new ArrayList<>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<>(map.values());
    }


    public List<List<String>> groupAnagrams1(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();

        for (String str : strs) {
            char[] chars = str.toCharArray();
            // 排序
            Arrays.sort(chars);
            String key = new String(chars);
            List<String> list = map.getOrDefault(key, new ArrayList<>());
            list.add(str);
            map.put(key, list);
        }
        return new ArrayList<>(map.values());
    }

}
